package test.teacher.login.controller;

import test.teacher.Teacher;
import test.teacher.common.utils.DBUtils;
import test.teacher.login.service.loginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @date 2018/5/18  16:25
 */

public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String id = request.getParameter("id");
        loginService login = new loginService();
       try {
           Teacher teacher = login.checkLogin(username, id);
           if (teacher == null) {
               //需要重登
               System.out.println("重新登录！"+username+","+id);
               request.getRequestDispatcher("index.jsp").forward(request, response);
           } else {
               //进入学生系统
               request.getSession().setAttribute("userInfo",teacher);

               Connection connection = null;
               try {
                   connection = DBUtils.getConnection2();
               } catch (ClassNotFoundException e) {
                   e.printStackTrace();
               }
               int idd = Integer.parseInt(id);
               String sql = "SELECT 学号,name,Tid from student where Tid="+idd;
               PreparedStatement pstm = connection.prepareStatement(sql);
               ResultSet resultSet = pstm.executeQuery();
               String student = "";
               while (resultSet.next()) {
                   student=student+(resultSet.getInt("学号"));
                   student=student+(resultSet.getString("name"));
                   student=student+(resultSet.getInt("Tid"));
               }
               request.setAttribute("student",student);
               request.getRequestDispatcher("WEB-INF/studentList.jsp").forward(request,response);
           }
       }catch (Exception e) {
           System.out.println(e);
       }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
